/**
 * DirenInternet namespace.
 */
 
if ("undefined" == typeof(DirenInternet)) {
  var DirenInternet = {};
};

DirenInternet.BrowserOverlay = {
    /*
     * In case a new page load, when the document of the page is ready, call checkUrl function.
     */
    init: function(event){
	if (event.target == gBrowser.contentDocument) {
	    DirenInternet.BrowserOverlay.checkUrl();
	}
    },
    /*
     * After a page load, check whether the url of the page is in the list. 
     */
    checkUrl : function(){
	var uri = new URI(window.top.getBrowser().selectedBrowser.contentWindow.location.href);
	var domain = uri.domain();
	var domainResource = uri.domain() + uri.resource();
	
	var key = null;
	if (domainResource in direninternet.list) {
	    key = domainResource;
	} else if (domain in direninternet.list) {
	    key = domain;     
	}
	
	if (!key) {
	    return;
	} else {
	    DirenInternet.BrowserOverlay.insertBanner(direninternet.list[key].name, direninternet.list[key].reason, key);
	}
    },
    /*
     * Insert a banner at the top of the document,
     * including the name and host of the page and the reason of the protest.
     */
    insertBanner : function(name, reason, host){	
	let div1 = window.content.document.createElement("div");
	var stringsBundle = document.getElementById("direninternet-string-bundle");
	var outerDivId = stringsBundle.getString('direninternet.id.outer_div');
	div1.setAttribute("id", outerDivId);
	div1.setAttribute("class", "sticky-wrapper");
	div1.setAttribute("style", "height: 240px;");
	
	let body = window.content.document.body;
	let docFirstChild = body.firstChild;
	
	var banner = window.content.document.createElement("div");
	banner.id = "direninternet_wrapper";
	banner.className = "direninternet_wrapper";
	    
	banner.innerHTML = 
	   "<div id='direninternet_inner' style='margin-left:auto;margin-right:auto;width: 60%;height: auto;padding: 10px;text-align: center;'>" + 
	   "  <p style='line-height:110%;color: white;text-align: center;margin:10px 5px 15px 20px;'>" + name + " (" + host + ")" + "</p> " + 
	   "  <p style='line-height:110%;color: white;text-align: center;margin:10px 5px 15px 20px;'>" + reason + " </p>" + 
	   "  <p style='line-height:110%;color: white;text-align: center;margin:10px 5px 15px 20px;'>" +
	   "    <span style='color:red;font-size:2em'>boykotediyoruz</span>" +
	   "  </p>" +
	   "  <p style='line-height:110%;color: white;text-align: center;margin:10px 5px 15px 20px;'>" +
	   "  <a style='color:white' href='https://code.google.com/p/direninternet/'>#direninternet</a></p>" + 
	   "</div>";
     
	banner.setAttribute("style", 
	    "display: block;" +
	    "color: white;" +
	    "font-size: 20px; " +
	    "font-family: arial; " +
	    "width: 100%; " +
	    "height: auto; " +
	    "background-color: black; " +
	    "vertical-align: middle; " +
	    "float: left;" +
	    "position:fixed;" +
	    "z-index: 2147483647;"  // Hack hack hack, to stop from this div being covered with random elements.
	);
	
	div1.appendChild(banner);
	
	if(docFirstChild){
	    body.insertBefore(div1,docFirstChild);
	} else {
	    body.appendChild(div1);
	}
    },
    /*
     * This function is depreciated.
     */
    checkBanner: function(){
	let body = window.content.document.body;
	if (body && body.hasChildNodes()) {
	    let docFirstChild = body.firstChild;
	    var stringsBundle = document.getElementById("direninternet-string-bundle");
	    var outerDivId = stringsBundle.getString('direninternet.id.outer_div');
	    if(docFirstChild.id == 'undefined' || docFirstChild.id !== outerDivId){
		return true;
	    }
	}
	return false;
    }
};

/*
 * Add load event listener to the browser so that it can detect any DOM content load
 */
gBrowser.addEventListener("DOMContentLoaded", DirenInternet.BrowserOverlay.init, true);